#! usr/bin/python 

def name_tyep(filename):

    def mprint(mytype):
        fw.write(mytype+"\n")
        
    fw=open("ref_trs_type.txt",'w') 

    f=open(filename+".sort.txt")
    for line in f.readlines():  
        NA_num=line.count("NA")
        gene_name=line.split("\t")[3]
        fw.write(gene_name+"\t")
        

        if NA_num==4:
            mprint("intergenic")


        if NA_num==2:
            left_z=line.split("\t")[5].split("|")[0]
            right_z=line.split("\t")[-1].split("|")[0]
            if line.split("|")[-1].strip("\n")=="NA":
                if int(line.split("|")[-3])<(-1000):
                    if left_z==right_z:
                        mprint("intergenic")
                    else:
                        mprint("biodirectional")
                else:
                    mprint("intergenic")
            else:
                if int(line.split("|")[-1])>1000:
                    if left_z==right_z:
                        mprint("intergenic")
                    else:
                        mprint("biodirectional")
                else:
                    mprint("intergenic")


        if NA_num==0:
            left=int(line.split("\t")[10].split("|")[1])
            right=int(line.split("|")[-1])
            left_zf=(line.split("\t")[10].split("|")[0]
            right_zf=(line.split("\t")[-1].split("|")[0]
            my_zf=(line.split("\t")[5].split("|")[0]
            min_num=min(-left,right)

            if min_num>=1000:
                mprint("intergenic")

            if 1000>min_num>0:
                #和距离最小的比正负
                if left==(-min_num):
                    if left_zf!=my_zf:
                        mprint("biodirectional")
                    else:
                        mprint("intergenic")  
                if right==min_num:
                    if right_zf!=my_zf:
                        mprint("biodirectional")
                    else:
                        mprint("intergenic")

            if min_num==0:  
                #判断是哪一边是0:
                if left==0 and right!=0:
                    if left_zf!=my_zf:
                        mprint("antisense")
                    else:
                        mprint("sense")
                if right==0 and left!=0:
                    if right_zf!=my_zf:
                        mprint("antisense")
                    else:
                        mprint("sense")
                if left==0 and right==0:
                    #两边都是0,和overlap多的那一边比较
                    left_dis=int(line.split("\t")[1])-int(line.split("\t")[6])
                    right_dis=int(line.split("\t")[2])-int(line.split("\t")[11])
                    if left_dis<right_dis:
                        if right_zf!=my_zf:
                            mprint("antisense")
                        else:
                            mprint("sense")
                    if left_dis>=right_dis:
                        if left_zf!=my_zf:
                            mprint("antisense")
                        else:
                            mprint("sense")
    fw.close()
    f.close()

if __name__ == '__main__':
    name_tyep("close.features")



#放到excell去重
#分别用1,10,100,1000替换

f2=open("name_type_useR_num.txt")
fw2=open("name_type_deredun.txt",'w')

dicn={}
f2.readline()
for line2 in f2.readlines()[0:26022]:#26023
    if dicn.has_key(line2.split("\t")[0]):
        value=int(line2.split("\t")[1].strip("\n"))+int(dicn[line2.split("\t")[0]])
        dicn[line2.split("\t")[0]]=value
    else:
        dicn[line2.split("\t")[0]]=int(line2.split("\t")[1].strip("\n"))

def nprint(this_type):
    fw2.write(this_type+"\n")

for t in dicn:
    fw2.write(t+"\t")
    if (dicn[t]/1000==1):
        nprint ("sense")
    elif(dicn[t]/100==1):
        nprint ("antisense")
    elif(dicn[t]/10==1):
        nprint ("biodirectional")
    elif(dicn[t]==1):
        nprint ("intergenic")
